import {AsyncStorage} from 'react-native'
import Request from "../utils/Request";
import constant from "../utils/constant";
import {default as RouterServer} from "../utils/RouterServer";
import {AuthModel} from "../interface/index";
import PushHandle from "../push.ios";


const {Actions} = require("react-native-router-flux");

const {Toast} = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_auth";


export default {
    namespace: 'auth',
    state: {
        isSignIn: false,
        sendButtonText: '发送验证码',
        sending: false,
        loading: false,
        coldDownTime: 10,
        profile: null
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {
        // 保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },
        //本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data), loading: false}
            });

            const auth: AuthModel = yield select(state => state.auth);

            if (auth.isSignIn) {
                PushHandle.setAlias(auth.profile.id);
                if (auth.profile.active === 0) {

                    yield call(RouterServer.push, 'createShop', {title: '创建店铺'})
                } else {
                    yield call(RouterServer.replace, 'tabbar', {title: '创建店铺'})
                }
            } else {
                PushHandle.setAlias('')
            }
        },
        //清除定时器
        * clearTimer({payload}, {put, select}) {
            yield put({
                type: 'save',
                payload: {sending: false, loading: false, sendButtonText: '获取验证码', coldDownTime: 10}
            })
        },
        //发送验证码
        * sendSms({payload}, {call, put, select}) {

            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {sending: true}
            });

            const data = yield call(Request.post, Request.getApi().sendSms, {...payload});
            if (data) {
                Toast.success('验证码已发送请注意查收', constant.ToastDuration);
            } else {
                yield put({
                    type: 'clearTimer'
                })
            }


        },
        //注册
        * signUp({payload}, {call, put, select}) {

            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });

            const data = yield call(Request.post, Request.getApi().signUp, {...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                yield call(Actions.pop);
            } else {
            }

            // yield put({
            //     type: 'save',
            //     payload: {sending: false}
            // });


        },
        //注册
        * signIn({payload}, {call, put, select}) {

            yield put({
                type: 'save',
                payload: {loading: true}
            });

            const data = yield call(Request.post, Request.getApi().signIn, {...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        profile: {...data.data.business},
                        isSignIn: true
                    }
                });
                const auth = yield select(state => state.auth);
                yield put({
                    type: 'saveToStorage',
                    payload: {...auth, loading: false}
                });
                if (data.data.business.active === 1) {
                    yield call(RouterServer.replace, 'tabbar');
                    PushHandle.setAlias(auth.profile.id);
                } else {
                    yield call(RouterServer.push, 'createShop', {title: '创建店铺'})
                }
            } else {
                yield put({type: 'logOut'})
            }
        },

        //创建店铺
        * createStore({payload}, {call, put, select}) {

            yield put({
                type: 'save',
                payload: {loading: true}
            });

            const data = yield call(Request.post, Request.getApi().createShop, {...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        profile: {...data.business},
                        isSignIn: false
                    }
                });
                const auth = yield select(state => state.auth);
                yield put({
                    type: 'saveToStorage',
                    payload: {...auth, loading: false}
                });
                if (data.data.active === 1) {
                    yield call(RouterServer.replace, 'tabbar')
                } else {
                    yield call(RouterServer.push, 'createShop', {title: '创建店铺'})
                }
            } else {
                console.log('error', data);
            }
        },

        //编辑店铺
        * editStore({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(Request.post, Request.getApi().changeShopInfo, {
                store_phone: auth.profile.store_phone,
                store_id: auth.profile.id, ...payload
            });
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                auth.profile = data.data;
                yield put({
                    type: 'save',
                    payload: {...auth}
                });
                yield put({
                    type: 'saveToStorage',
                    payload: {...auth, loading: false}
                });
            } else {
                console.log('error', data);
            }
        },


        //登出
        * logOut({payload}, {call, put, select}) {
            yield call(RouterServer.replace, 'auth');
            yield put({
                type: 'save',
                payload: {
                    isSignIn: false,
                    profile: null
                }
            });
            const auth = yield select(state => state.auth);
            yield put({
                type: 'saveToStorage',
                payload: {...auth}
            });
        },

        //修改密码
        * changePassword({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {
                    loading: true,
                }
            });
            const data = yield call(Request.post, Request.getApi().changePwd, payload);
            yield put({
                type: 'save',
                payload: {
                    loading: false
                }
            });
            if (data) {
                Toast.success('修改成功!', constant.ToastDuration);
                yield call(RouterServer.replace, 'auth');
                yield put({
                    type: 'save',
                    payload: {
                        loading: false,
                        isSignIn: false,
                        profile: null
                    }
                });
            }

            // const auth = yield select(state => state.auth);
            // yield put({
            //     type: 'saveToStorage',
            //     payload: {...auth}
            // });


        },

        //更新余额信息
        * upDataBalance({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {
                    loading: true,
                }
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(Request.get, Request.getApi().totalMoney, {store_id: auth.profile.id, ...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                auth.profile.store_collect = data.data;
                yield put({
                    type: 'save',
                    payload: {...auth, loading: false}
                });
                yield put({
                    type: 'saveToStorage',
                    payload: {...auth, loading: false}
                });
            }

            // const auth = yield select(state => state.auth);
            // yield put({
            //     type: 'saveToStorage',
            //     payload: {...auth}
            // });


        },
        //更新余额信息
        * doWithDraw({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {
                    loading: false,
                }
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(Request.post, Request.getApi().withdrawApply, {store_id: auth.profile.id, ...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                Toast.success('提现申请已经提交', constant.ToastDuration);
                yield put({
                    type: 'upDataBalance'
                })
            }
        },

        //
    },
    subscriptions: {
        setup({dispatch}) {
            dispatch({
                type: 'restoreFromStorage'
            })
        }
    },
};
